package ar.edu.unrn.lia.bean.datamodel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

import ar.edu.unrn.lia.model.Alquiler;
import ar.edu.unrn.lia.model.Movimiento;
import ar.edu.unrn.lia.service.AlquilerService;



public class LazyAlquilerDataModel extends LazyDataModel<Alquiler> {

	private static final long serialVersionUID = 1L;
	
	private static final String ORDER_DEFAULT = "id";

	private List<Alquiler> list;

	@Inject
	AlquilerService entityService;
	
	

	public AlquilerService getEntityService() {
		return entityService;
	}

	public LazyAlquilerDataModel(AlquilerService entityService) {
		super();
		this.entityService = entityService;
	}

	@Override
	public List<Alquiler> load(int startingAt, int maxPerPage, String sortField,
			SortOrder sortOrder, Map<String, String> filters) {
		try {
			try {
				
				list = getEntityService().getList(startingAt, maxPerPage,filters, (sortField==null?ORDER_DEFAULT:sortField), (sortOrder.name().equals(SortOrder.ASCENDING.toString())));

			} finally {

			}
		} catch (Exception e) {
			e.printStackTrace();
			list = new ArrayList<Alquiler>(0);
		}

		// set the total of list
		if (getRowCount() <= 0) {
			setRowCount(getEntityService().getCount(filters).intValue());
		}

		// set the page dize
		setPageSize(maxPerPage);

		return list;
	}

	@Override
	public Object getRowKey(Alquiler entity) {
		return entity.getId();
	}

	@Override
	public Alquiler getRowData(String entityId) {
		Integer id = Integer.valueOf(entityId);

		for (Alquiler entity : list) {
			if (id.equals(entity.getId())) {
				return entity;
			}
		}

		return null;
	}
}